<?php

/**
 * @author ThaiNM
 */
class SqlUserTable {

    static function SqlSelectUser($email) {
        $where = '';
        if ($email != '')
            $where = ' WHERE email = ? ';

        $sql = " SELECT
                    userlogin.deviceId device_id,
                    userlogin.apiToken api_token,
                    userlogin.logTime login_time,
                    userlogin.activeTime active_time,
                    `user`.facebookID faceboke_id,
                    `user`.email,
                    `user`.fName first_name,
                    `user`.lName last_name,
                    `user`.cpf,
                    `user`.photo,
                    `user`.city,
                    `user`.lastLongtitude last_longtitude,
                    `user`.lastLattitude last_lattitude,
                    `user`.dob,
                    `user`.sex,
                    `user`.allowAdult allow_adult,
                    `user`.userPhoto user_photo,
                    `user`.`status`,
                    `user`.apiKey api_key
                    FROM
                    `user`
                    LEFT JOIN userlogin ON userlogin.userId = `user`.id
                    ";
        $sql .= $where;

        return $sql;
    }

    static function SqlSelectUserById($userId) {
        $where = '';
        if ($userId != '')
            $where = " WHERE id = ? ";

        $sql = " SELECT
                    `user`.facebookID faceboke_id,
                    `user`.email,
                    `user`.fName first_name,
                    `user`.lName last_name,
                    `user`.cpf,
                    `user`.photo,
                    `user`.city,
                    `user`.lastLongtitude last_longtitude,
                    `user`.lastLattitude last_lattitude,
                    `user`.dob,
                    `user`.sex,
                    `user`.allowAdult allow_adult,
                    `user`.userPhoto user_photo,
                    `user`.`status`,
                    `user`.apiKey api_key
                    FROM
                    `user`
                    ";
        $sql .= $where;

        return $sql;
    }
    
    static function SqlSelectUserByApiToken() {

        $sql = " SELECT
                    `user`.facebookID faceboke_id,
                    `user`.email,
                    `user`.fName first_name,
                    `user`.lName last_name,
                    `user`.cpf,
                    `user`.photo,
                    `user`.city,
                    `user`.lastLongtitude last_longtitude,
                    `user`.lastLattitude last_lattitude,
                    `user`.dob,
                    `user`.sex,
                    `user`.allowAdult allow_adult,
                    `user`.userPhoto user_photo,
                    `user`.`status`,
                    `user`.apiKey api_key
                    FROM
                    `user`
                    WHERE `user`.apiKey = ?
                    ";

        return $sql;
    }

    static function SqlRegisterUser() {

        $sql = ' INSERT INTO user(email, password, apiKey, status) values(?, ?, ?, 0) ';
        return $sql;
    }

    static function SqlInsertUser() {
        return "INSERT INTO `user` (
                        `user`.facebookID,
                        `user`.email,
                        `user`.photo,
                        `user`.city,
                        `user`.lastLongtitude,
                        `user`.lastLattitude,
                        `user`.dob,
                        `user`.sex,
                        `user`.allowAdult,
                        `user`.cpf,
                        `user`.fName,
                        `user`.lName,
                        `user`.`password`,
                        `user`.userPhoto,
                        `user`.`status`,
                        `user`.apiKey
                )
                VALUES
                        (
                            ?,?,?,?,?,?,?,?,?,?,?,?,?,?, 1 ,?
                        )";
    }

    static function SqlLogin() {
        return "SELECT id,password FROM user WHERE email = ?";
    }

    static function SqlInsertUserLogin() {
        return "INSERT INTO userlogin (
                                    userId,
                                    apiToken,
                                    logTime,
                                    activeTime
                            )
                            VALUES
                                    (
                                            ?,
                                            ?,
                                            CURRENT_TIMESTAMP (),
                                            DATE_ADD(
                                                    CURRENT_TIMESTAMP (),
                                                    INTERVAL 1 DAY
                                            )
                                    )";
    }

    static function SqlDeleteUserLogin() {
        return "DELETE
                FROM
                        userlogin
                WHERE
                        userlogin.userId = ?";
    }

    static function SqlGetIdUserbyToken() {
        return "SELECT
                        userlogin.userId
                FROM
                        userlogin
                WHERE
                        userlogin.apiToken = ?
                AND userlogin.activeTime >= CURRENT_TIMESTAMP ()";
    }

    /**
     * 
     * @return Sql Update user location
     */
    static function SqlUpdateUserLocation() {
        return "UPDATE `user`
                SET `user`.lastLongtitude = ? , `user`.lastLattitude = ?
                WHERE
                        `user`.id = ?";
    }
    
    static function SqlUpdateUserPhoto() {
        return "UPDATE `user`
                SET `user`.userPhoto = ? 
                WHERE
                        `user`.id = ?";
    }


    static function SqlActiveUser() {
        return "UPDATE `user`
                SET `user`.status = 1 
                WHERE
                        `user`.apiKey = ?";
    }

    static function SqlSelectUserByFacebookId() {
        return "SELECT
                        id
                FROM
                        user
                WHERE
                        facebookID = ?";
    }

}
